<template>
  <div>
    <mini-head :title="$route.name" />
    <van-address-edit
      :area-list="areaList"
      show-set-default
      show-delete
      show-postal
      tel-maxlength="11"
      area-placeholder
      @save="onSave"
      @change-area="cheak"
      @delete="onDelete"
      :address-info="{
          name: info.name,
          tel: info.tel,
          addressDetail: info.addressDetail,
          postalCode: info.postalCode,
          //province: info.province, // 省
          //city: info.city, //市
          //county: info.county, // 区
          isDefault: info.isDefault,
          areaCode: info.areaCode // 省市区编码
      }"
    />
  </div>
</template>

<script>
import { areaList } from '@vant/area-data'
import { linkmanDetails, deleteLinkman, setLinkman } from '_api'
import { Toast } from 'vant'
import MiniHead from '../../components/MiniHead.vue'
import { gettoken } from '../../utils'
export default {
  components: { MiniHead },
  data () {
    return {
      info: {
        name: '',
        tel: '',
        addressDetail: '',
        postalCode: '',
        province: '',
        city: '',
        county: '',
        isDefault: '',
        areaCode: ''
      },
      areaList,
      areaInfo: '', // 省市区名字
      conner: {
        id: '',
        linkMan: '',
        mobile: '',
        address: '',
        code: '',
        cityId: '', // 市编码
        provinceId: '', // 省编码
        districtId: '', // 区编码
        isDefault: false
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    // 初始值
    init () {
      linkmanDetails({
        id: this.$route.query.id,
        token: gettoken()
      }).then((res) => {
        console.log(res)
        if (res.data.code === 0) {
          this.info.name = res.data.data.info.linkMan
          this.info.tel = res.data.data.info.mobile
          this.info.addressDetail = res.data.data.info.address
          this.info.postalCode = res.data.data.info.code
          this.conner.provinceId = res.data.data.info.provinceId
          this.conner.cityId = res.data.data.info.cityId
          this.conner.districtId = res.data.data.info.districtId
          //   this.info.province = res.data.data.info.provinceStr
          //   this.info.city = res.data.data.info.cityStr
          //   this.info.county = res.data.data.info.areaStr
          this.info.isDefault = res.data.data.info.isDefault
          this.conner.id = res.data.data.info.id
          //  拿取默认地址的code  设置默认的省市区
          //   this.areaCode = res.data.data.info.provinceId
          //   this.areaCode = res.data.data.info.cityId
          //   this.areaCode = res.data.data.info.districtId

          //   console.log(res.data.data.info.provinceId + '' + res.data.data.info.cityId + '' + res.data.data.info.districtId)
          //   console.log(Object.prototype.toString.call(res.data.data.info.provinceId + '' + res.data.data.info.cityId + '' + res.data.data.info.districtId))
          this.info.areaCode = res.data.data.info.districtId // 特码直接上区的ID

        //   this.info.areaCode = {
        //     provinceId: res.data.data.info.provinceId,
        //     cityId: res.data.data.info.cityId,
        //     districtId: res.data.data.info.districtId
        //   }
        }
      })
    },
    cheak (e) {
      this.conner.provinceId = e[0].code
      this.conner.cityId = e[1].code
      this.conner.districtId = e[2].code
    },
    // 删除
    onDelete () {
      deleteLinkman({
        id: this.conner.id,
        token: gettoken()
      }).then((res) => {
        if (res.data.code === 0) {
          console.log(res)
          Toast('删除成功')
          this.$router.go(-1)
        }
      })
    },
    onSave (e) {
    //   console.log(e)
      this.conner.address = e.addressDetail
      this.conner.linkMan = e.name
      this.conner.mobile = e.tel
      this.conner.code = e.postalCode
      this.conner.isDefault = e.isDefault
      this.areaInfo = e.province + e.city + e.county
      setLinkman({
        ...this.conner,
        token: gettoken()
      }).then((res) => {
        console.log('11111')
        if (res.data.code === 0) {
          console.log(res)
          Toast('修改成功')
          this.$router.go(-1)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
